package org.bulldog.playlist.program;

import org.bulldog.playlist.util.DateUtil;

import java.util.HashMap;
import java.util.Map;


/**
 * 节目
 *
 * @author zhanghongbin
 * @version 1.0
 */
public final class Program implements Cloneable {

    private String id = "";
    private String url = "";
    private String startTime = "";
    private String endTime = "";
    //节目时长(秒)
    private int duration = 0;
    //节目类型 0 普通节目，1 插播节目 2 日常插播节目
    private int level = -1;
    private String name = "";
    //点播结束时间
    private String demandEndTime = "";
    // 播放时长
    private int playDuration = 0;

    private Map<String, Object> params = new java.util.HashMap();

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(String key, String value) {
        this.params.put(key, value);
    }

    public Program() {
    }

    /**
     * 插播节目
     *
     * @param id
     * @param url
     * @param startTime 节目开始时间 HH:mm:ss
     * @param endTime   节目结束时间 HH:mm:ss
     */
    public Program(String id, String name, String url, String startTime, String endTime) {
        this.id = id;
        this.url = url;
        this.startTime = startTime;
        this.endTime = endTime;
        this.name = name;
        this.level = 1;
    }

    /**
     * 日常插播节目
     *
     * @param id
     * @param name
     * @param url
     * @param startTime
     * @param endTime
     * @param duration
     */
    public Program(String id, String name, String url, String startTime, String endTime, int duration) {
        this.id = id;
        this.url = url;
        this.startTime = startTime;
        this.endTime = endTime;
        this.name = name;
        this.level = 2;
        this.duration = duration;
        this.playDuration = duration;
    }

    /**
     * 日常节目
     *
     * @param id
     * @param url
     * @param duration 时长
     */
    public Program(String id, String name, String url, int duration) {
        this.id = id;
        this.url = url;
        this.duration = duration;
        this.name = name;
        this.level = 0;
    }

    public int getPlayDuration() {
        return playDuration;
    }

    void setLevel(int level) {
        this.level = level;
    }

    void setPlayDuration(int playDuration) {
        this.playDuration = playDuration;
    }

    void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    void setDuration(int duration) {
        this.duration = duration;
        this.playDuration = duration;
    }

    void setDemandEndTime(String demandEndTime) {
        this.demandEndTime = demandEndTime;
    }

    public String getUrl() {
        return url;
    }

    public int getLevel() {
        return level;
    }

    public String getStartTime() {
        return startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public int getDuration() {
        return duration;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return this.name;
    }

    public String getDemandEndTime() {
        return this.demandEndTime;
    }

    @Override
    public int hashCode() {
        //不能放到hash集合里边hashCode没有重写
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof Program) {
            Program tmp = (Program) obj;
            return DateUtil.isBetween(this.getStartTime(), tmp.startTime, tmp.endTime);
        }
        return false;
    }

    @Override
    public String toString() {
        String paramsString = this.params.toString();
//        if (this.params.isEmpty()) {
//            paramsString = this.params.toString();
//        } else {
////            paramsString = this.params.toString().replaceAll("\\{", "{\"").replaceAll("=", "\"=\"").
////                    replaceAll(",\\s", "\",\"").replaceAll("}", "\"}").replaceAll("=", ":");
//        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{\"id\":" + "\"" + this.id + "\",").
                append("\"name\":" + "\"" + this.name + "\",").
                append("\"url\":" + "\"" + this.url + "\",").append("\"startTime\":\"" + this.startTime + "\",")
                .append("\"endTime\":\"" + this.endTime + "\",").append("\"demandEndTime\":\"" + this.demandEndTime + "\",").append("\"duration\":" + this.duration + ",")
                .append("\"playDuration\":" + this.playDuration + ",").append("\"level\":" + this.level + ",\"params\":" + paramsString + "}");
        return stringBuilder.toString();
    }

    @Override
    public Object clone() {
        try {
            Program program = (Program) super.clone();
            program.params = (Map) ((HashMap) program.params).clone();
            return program;
        } catch (Exception e) {
            e.printStackTrace();
            return new Program();
        }
    }
}
